from django.db import models

# Create your models here.
class UserInfo(models.Model):
    """
    用户表
    """
    name = models.CharField(max_length=30, verbose_name='用户名')  # 名称
    password = models.CharField(max_length=30, default=123456, verbose_name='密码')  # 密码
    roles = models.ManyToManyField("Role")  # 连接Role表

    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = "用户"


class Role(models.Model):
    role_name = models.CharField(max_length=100, verbose_name='角色')  # 角色名称
    permissions = models.ManyToManyField("Permission", verbose_name='权限')

    def __str__(self):
        return self.role_name
    class Meta:
        verbose_name_plural = "角色"

class Permission(models.Model):
    permission_name = models.CharField(max_length=100, verbose_name='权限名称')  # 权限名称
    url = models.CharField(max_length=100, verbose_name='路由')  # 路由
    menu = models.ForeignKey("Menu", null=True, blank=True, on_delete=models.CASCADE)

    def __str__(self):
        return  self.permission_name
    class Meta:
        verbose_name_plural = "权限"

class Menu(models.Model):
    title = models.CharField(max_length=100, unique=True, verbose_name='菜单')
    parent = models.ForeignKey("Menu", null=True, blank=True, on_delete=models.CASCADE, verbose_name='所属关系')

    def __str__(self):
        title_list = [self.title]
        p = self.parent
        while p:
            title_list.insert(0, p.title)
            p = p.parent
        return '-'.join(title_list)
    class Meta:
        verbose_name_plural = "菜单"